﻿using System;
using System.Collections;
using System.Data;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace SDT.Libraries.Utilities
{
    /// <summary>
    /// Class provides methods for working with Validation.
    /// </summary>
    public class ValidationHelper
    {
        #region IsEmpty method
        /// <summary>
        /// Check if the object has a value.
        /// </summary>
        public static bool IsEmpty(HtmlInputText txtInput)
        {
            if (txtInput == null)
                return true;
            return string.IsNullOrEmpty(txtInput.Value.Trim());
        }

        public static bool IsEmpty(string input)
        {
            if (input == null)
                return true;
            return string.IsNullOrEmpty(input.Trim());
        }

        public static bool IsEmpty(TextBox txtInput)
        {
            if (txtInput == null)
                return true;
            return string.IsNullOrEmpty(txtInput.Text.Trim());
        }

        public static bool IsEmpty(HtmlSelect cboInput)
        {
            if (cboInput == null)
                return true;
            return string.IsNullOrEmpty(cboInput.Value.Trim());
        }

        public static bool IsEmpty(DropDownList cboInput)
        {
            if (cboInput == null)
                return true;
            return string.IsNullOrEmpty(cboInput.SelectedValue.Trim());
        }

        public static bool IsEmpty(ListBox cboInput)
        {
            if (cboInput == null)
                return true;
            return string.IsNullOrEmpty(cboInput.SelectedValue);
        }
        #endregion

        public static bool IsContainSpace(string input)
        {
            return input.Contains(" ");
        }

        public static bool IsNumber(string input)
        {
            try
            {
                int.Parse(input);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool IsValidEmailFormat(string inputEmail)
        {
            if ((inputEmail == null) || (inputEmail == string.Empty))
            {
                return false;
            }

            string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
            Regex re = new Regex(strRegex);

            return re.IsMatch(inputEmail);
        }

        public static bool IsValidUSPhoneNumber(string strPhone)
        {
            // Allows phone number of the format: NPA = [2-9][0-8][0-9] Nxx = [2-9][0-9][0-9] Station = [0-9][0-9][0-9][0-9]
            return new Regex(@"^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$").IsMatch(strPhone);
        }

        public static bool IsValidSGPhoneNumber(string phone)
        {
            return new Regex(@"^[6]\d{7}$").IsMatch(phone);
        }

        public static bool IsValidURL(string strURL)
        {
            // Allows HTTP and FTP URL's, domain name must start with alphanumeric and can contain a port number
            // followed by a path containing a standard path character and ending in common file suffixies found in URL's
            // and accounting for potential CGI GET data
            Regex re = new Regex(@"^^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_=]*)?$");
            return re.IsMatch(strURL);
        }

        public static bool HasRow(DataTable input)
        {
            return ((input != null) && (input.Rows.Count > 0));
        }

        public static int PaserInteger(object input)
        {
            try
            {
                if (input == null)
                    return 0;

                return int.Parse(input.ToString());
            }
            catch
            {
                return 0;
            }
            
        }

        //public static bool IsNotInvalidDate(DateTime argument)
        //{
        //    return (!argument.IsValid());
        //}
    }
}
